package com.ibm.ba.demo.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.ibm.ba.demo.entity.RegisterRequest;
import com.ibm.ba.demo.entity.User;
import com.ibm.ba.demo.service.RegisterService;

@Controller
@SessionAttributes("userinfo")
public class RegisterController extends AbstractController {

	// private static String SCORE_VIEW_NAME = "get_scores";
	private static String REG_VIEW_NAME = "registerres";
	private static Logger logger = Logger.getLogger(RegisterRequest.class);

	@Autowired
	private RegisterService registerService;

	/**
	 * Map request URI - '/login' to method - 'login()'. Login the ba demo
	 * portal.
	 */
	@RequestMapping(value = "/register", params = "method=registeraa", method = RequestMethod.POST)
	public String Register(ModelMap model,
			@ModelAttribute RegisterRequest request) {

		System.out.println("register controller here" + request.getUsername());
		model.addAttribute(
				"userinfo",
				registerService.RegisterUser(request.getUsername(),
						request.getPasswd(), request.getEmail()));
		logger.info("login(ModelMap model) begin");
		logger.info("login(ModelMap model) end");
		// return SCORE_VIEW_NAME;
		return REG_VIEW_NAME;
	}

	@RequestMapping(value = "/register2", params = "method=register", method = RequestMethod.POST)
	public String Register2(ModelMap model,
			@ModelAttribute("userinfo") User user) {

		System.out.println("register2 controller here modelAttribhute"
				+ user.getUsername());
		logger.info("login(ModelMap model) begin");
		logger.info("login(ModelMap model) end");
		// return SCORE_VIEW_NAME;
		return REG_VIEW_NAME;
	}
}
